package com.exam.bean.subject;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.exam.bean.HardEasy;
import com.exam.bean.QuestionType;
import com.exam.bean.paper.AnswerPaper;
import com.exam.bean.paper.PaperQuestionType;

/**
 * 试题
 *
 */
@Entity
public class Question implements Serializable{
	
	private static final long serialVersionUID = 4426178008327850010L;

	private Integer id;
	private QuestionType type;//题型
	private String title;//题目
	private String addtitle;//附加标题
	private String answer;//标准答案
	private String answeranalyze;//答案解析
	private KnowledgePoint point;//知识点
	private HardEasy hardEasy;//难易程度
	private Integer appearCount=1;//曝光度
	private Boolean visible=true;
	private Date createdate = new Date();//创建时间
	private Set<PaperQuestionType> papertypes=new HashSet<PaperQuestionType>();//试卷题型
	private Set<OptionItem> items=new HashSet<OptionItem>();//选项
	private Set<AnswerPaper> answers=new HashSet<AnswerPaper>();
	
	public Question() {}

	public Question(Integer id) {
		this.id = id;
	}

	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false,length=15)
	public QuestionType getType() {
		return type;
	}
	public void setType(QuestionType type) {
		this.type = type;
	}

	@Lob 
	@Column(nullable=false)
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

	@Lob 
	public String getAddtitle() {
		return addtitle;
	}
	public void setAddtitle(String addtitle) {
		this.addtitle = addtitle;
	}

	@Column(length=400,nullable=false)
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}

	@Lob 
	public String getAnsweranalyze() {
		return answeranalyze;
	}
	public void setAnsweranalyze(String answeranalyze) {
		this.answeranalyze = answeranalyze;
	}

	@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
	@JoinColumn(name="pointid")
	public KnowledgePoint getPoint() {
		return point;
	}
	public void setPoint(KnowledgePoint point) {
		this.point = point;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false,length=8)
	public HardEasy getHardEasy() {
		return hardEasy;
	}
	public void setHardEasy(HardEasy hardEasy) {
		this.hardEasy = hardEasy;
	}

	@Column(nullable=false)
	public Integer getAppearCount() {
		return appearCount;
	}
	public void setAppearCount(Integer appearCount) {
		this.appearCount = appearCount;
	}

	@Column(nullable=false)
	public Boolean getVisible() {
		return visible;
	}
	public void setVisible(Boolean visible) {
		this.visible = visible;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	public Date getCreatedate() {
		return createdate;
	}
	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	@ManyToMany(mappedBy="questions", cascade=CascadeType.REFRESH)
	public Set<PaperQuestionType> getPapertypes() {
		return papertypes;
	}
	public void setPapertypes(Set<PaperQuestionType> papertypes) {
		this.papertypes = papertypes;
	}

	@OneToMany(mappedBy="itemquestion",cascade=CascadeType.ALL,fetch = FetchType.EAGER)
	public Set<OptionItem> getItems() {
		return items;
	}
	public void setItems(Set<OptionItem> items) {
		this.items = items;
	}

	@OneToMany(mappedBy="question",cascade=CascadeType.REMOVE)
	public Set<AnswerPaper> getAnswers() {
		return answers;
	}
	public void setAnswers(Set<AnswerPaper> answers) {
		this.answers = answers;
	}

	public void addItem(OptionItem item){
		if(!this.items.contains(item)) {
			item.setItemquestion(this);
			this.items.add(item);System.out.println(item.getOptions());
		}
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Question other = (Question) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
}
